<?php 
namespace Admin\Controller;
use Think\Controller;
class LoginController extends Controller{
	//登陆页面
    public function index(){
		$this->display();
    }
    //用户登陆处理表单
    public function login(){  	
    	//if(!IS_POST) $this->error("非法操作!");
		$db = M('user');
		$admin = M('admin');
		$username =I('username');
		$password =I('password','','md5');
		$code = I('verify','','strtolower');
		//判断验证码是否正确
		if($code != session('verify')){
			$this->error("验证码错误");
		}
		//判断用户是都存在
		$user = $db->where(array('username'=>$username,'password'=>$password,))->find();
		//判断用户是否为管理员
		$isadmin = $admin->where(array('name'=>$username,))->find();
		
		if (!$user) {
			$this->error('账号或密码错误');
		}
		if($user['lock'] == '1'){
			$this->error('用户被锁定,请联系管理员解锁');
		}
		if(!$isadmin){
			$this->error("不是管理员你登陆个毛!");
		}
		//dump($isadmin);die;
		//更新数据信息
		$data =array( 
			'id' => $user['id'],
			'logintime' => time(),
			'loginip' => get_client_ip(),
		);	
		$db->save($data); 
		//把用登陆信息存到SESSION
		session("id",$user['id']);
		session('isadmin',$isadmin['id']);
		session("username",$isadmin['name']);
		session("logintime",date('Y-m-d H:i:s'),$user['logintime']);
		session("loginip",$user['loginip']);
		redirect(__APP__.'/Admin/');					 
    }

    //验证码
    public function verify(){
    	import('Util.Image',APP_PATH);
    	\Util\Image::verify();
    }

}
?>